package com.a918.service.impl;

import com.a918.dao.*;
import com.a918.dto.ContentDto;
import com.a918.dto.FrontUnionDto;
import com.a918.dto.PublishData;
import com.a918.entity.*;
import com.a918.dao.CommonintroMapper;
import com.a918.dao.ContentMapper;
import com.a918.dao.ContentTypeMapper;
import com.a918.dao.CoverMapper;
import com.a918.entity.Commonintro;
import com.a918.entity.Content;
import com.a918.entity.Cover;
import com.a918.dto.CoverData;
import com.a918.service.FrontService;
import com.a918.util.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class FrontServiceImp implements FrontService {

    private CoverMapper coverMapper;
    private CommonintroMapper commonintroMapper;
    private ContentMapper contentMapper;
    private ContentTypeMapper contentTypeMapper;
    private FrontUnionMapper frontUnionMapper;
    private FrontMemberMapper frontMemberMapper;
    private UserMapper userMapper;

    @Autowired
    public FrontServiceImp(CoverMapper coverMapper, CommonintroMapper commonintroMapper, ContentMapper contentMapper, ContentTypeMapper contentTypeMapper, FrontUnionMapper frontUnionMapper, FrontMemberMapper frontMemberMapper, UserMapper userMapper) {
        this.coverMapper = coverMapper;
        this.commonintroMapper = commonintroMapper;
        this.contentMapper = contentMapper;
        this.contentTypeMapper = contentTypeMapper;
        this.frontUnionMapper = frontUnionMapper;
        this.frontMemberMapper = frontMemberMapper;
        this.userMapper = userMapper;
    }

    @Override
    public PageInfo<CoverData> getAllCoverData(@RequestParam Map<String, String> paramMap) {

        int pno = Integer.parseInt(paramMap.get("page"));
        int pageSize = Integer.parseInt(paramMap.get("limit"));
        PageHelper.startPage(pno, pageSize);
        // 1. 开启分页
        List<Cover> coverPoList = coverMapper.selectAll();
        // 3. 封装list到PageInfo对象中自动分页
        PageInfo<Cover> coverPoPageInfo = new PageInfo<>(coverPoList);
        // 4. 转换为UserVo类型的PageInfo对象
        PageInfo<CoverData> coverVoPageInfo = PageUtils.PageInfo2PageInfoVo(coverPoPageInfo);
        // 5. 创建需要分页的UserVoList
        List<CoverData> coverVoList = new ArrayList<>();
        // 6. 遍历UserPo给UserVoList初始化
        for(Cover coverpo : coverPoList){
            CoverData covervo = new CoverData();
            covervo.setId(coverpo.getId());
            covervo.setTitle(contentMapper.selectByPrimaryKey(coverpo.getContentid()).getTitle());
            covervo.setPic(coverpo.getPic());
            coverVoList.add(covervo);
        }
        for (CoverData uservo : coverVoList) {
            coverVoPageInfo.getList().add(uservo);
        }
        return coverVoPageInfo;
    }

    @Override
    public List<Cover> getAllCover() {
        return coverMapper.selectAll();
    }

    @Override
    public String getCommonIntro(String type) {
        return commonintroMapper.selectContent(type);
    }

    @Override
    public Content getContentById(Integer id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer gettypeIdByCode(String type) {
        return contentTypeMapper.gettypeIdByCode(type);
    }

    @Override
    public void deleteCoverById(Integer id) { coverMapper.deleteByPrimaryKey(id); }

    @Override
    public void insertCover(Cover cover) { coverMapper.insert(cover); }

    @Override
    public Cover selectCoverById(Integer id) { return coverMapper.selectByPrimaryKey(id); }

    @Override
    public FrontUnion getContextById(Integer id){return frontUnionMapper.selectByPrimaryKey(id);}

    @Override
    public List<FrontMember> getMemberByStatus(String status) { return frontMemberMapper.getMemberListByStatus(status); }

    @Override
    public FrontMember getIntroductById(Integer id){return frontMemberMapper.selectByPrimaryKey(id);}

    @Override
    public User getNameById(Integer id){return userMapper.selectByPrimaryKey(id);}

    @Override
    public PageInfo<FrontUnionDto> getCheckedUnion() {
        List<FrontUnion> unionList = frontUnionMapper.getUnionListByStatus("yfb");
        PageInfo<FrontUnion> unionPageInfo = new PageInfo<>(unionList);
        PageInfo<FrontUnionDto> res = PageUtils.PageInfo2PageInfoVo(unionPageInfo);
        List<FrontUnionDto> unionDtolist = new ArrayList<>();
        for (FrontUnion union : unionList) {
            FrontUnionDto frontUnionDto = new FrontUnionDto();
            frontUnionDto.setId(union.getId());
            frontUnionDto.setCreattime(union.getCreattime());
            frontUnionDto.setUsername(userMapper.selectByPrimaryKey(union.getUserid()).getUsername());
            frontUnionDto.setPicid(union.getPicid());
            frontUnionDto.setContext(union.getContext());
            unionDtolist.add(frontUnionDto);
        }
        for (FrontUnionDto i : unionDtolist) {
            res.getList().add(i);
        }
        return res;

    }

    @Override
    public PageInfo<ContentDto> getCheckedContent(Map mapParameter) {
        List<Content> contentList = contentMapper.getContentList(mapParameter);
        PageInfo<Content> contentPageInfo = new PageInfo<>(contentList);
        PageInfo<ContentDto> res = PageUtils.PageInfo2PageInfoVo(contentPageInfo);
        List<ContentDto> contentDtolist = new ArrayList<>();
        for (Content content : contentList) {
            ContentDto contentDto = new ContentDto();
            contentDto.setId(content.getId());
            contentDto.setTypeid(content.getTypeid());
            contentDto.setAddtime(content.getAddtime());
            contentDto.setAdduserid(content.getAdduserid());
            contentDto.setPublishtime(content.getPublishtime());
            contentDto.setUsername(userMapper.selectByPrimaryKey(content.getAdduserid()).getUsername());
            contentDto.setContext(content.getContext());
            contentDto.setTitle(content.getTitle());
            contentDto.setViewnum(content.getViewnum());
            contentDto.setWeight(content.getWeight());
            contentDtolist.add(contentDto);
        }
        for (ContentDto i : contentDtolist) {
            res.getList().add(i);
        }
        return res;

    }

    @Override
    public List<Content> getRecommend(Map mapParameter) {
        return contentMapper.getRecommendList(mapParameter);
    }

    @Override
    public void UpdateViewnum(Content content){contentMapper.updateViewnum(content);}

    @Override
    public void deleteCoverByContentid(Integer id) { coverMapper.deleteCoverByContentid(id); }

}
